package com.power.sifa.account.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.account.api.IHa_depart_accountFeign;
import com.account.api.IHa_user_accountFeign;
import com.alibaba.fastjson.JSON;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.CommonUtils;
import com.plf.core.base.model.Ha_depart;
import com.plf.core.base.model.Ha_user;
import com.power.sifa.account.dao.Ha_user_approveDao;
import com.power.sifa.account.model.pageInfo.Ha_user_approvePageInfo;
import com.power.sifa.account.service.Ha_user_approveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class IHa_user_approveServiceImpl implements Ha_user_approveService {

    @Resource
    private Ha_user_approveDao ha_user_approveDao;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<Map<String, Object>> findUserTree(String departid, Ha_user_approvePageInfo pageInfo) {
        // 部门和单位所关联的字段不同
        if(pageInfo.getType() == 1) {
            pageInfo.setJoinfield("sectionid");
        } else {
            pageInfo.setJoinfield("departid");
        }

        // 1. 根据条件筛选部门
        List<Ha_depart>  departList = this.ha_user_approveDao.queryDepartList(departid, pageInfo);
        List<Map<String, Object>> list = new ArrayList<>();
        departList.stream().forEach(item -> {
            list.add(BeanUtil.beanToMap(item));
            list.get(list.size() - 1).put("type", "depart");
        });
        List<Map<String, Object>> result = CommonUtils.createTree(list, "id", "parentid", false, null);
        if(!CollectionUtils.isEmpty(departList) && pageInfo.getIsuser()) {
            List<String> parentIds = new ArrayList<>();
            departList.stream().forEach(v -> {
                parentIds.add(v.getId());
            });

            // 2. 查询部门所属用户
            List<Map>  userList = this.ha_user_approveDao.queryUserByDepartids(parentIds, pageInfo);
            if(!CollectionUtils.isEmpty(userList)) {
                Map<String, List<Map>> departUserMap = new HashMap(16);
                List<Map> tempList;
                for (Map v : userList) {
                    if(null != departUserMap.get(v.get(pageInfo.getJoinfield()))) {
                        tempList = departUserMap.get(v.get(pageInfo.getJoinfield()));
                    } else  {
                        tempList = new ArrayList<>(10);
                    }
                    tempList.add(v);
                    departUserMap.put((String) v.get(pageInfo.getJoinfield()), tempList);
                }
                result = this.recursion(departid, result, departUserMap, pageInfo);
            }
        }

        return result;
    }

    List<Map<String, Object>> recursion(String departid, List<Map<String, Object>> result, Map<String, List<Map>> departUserMap, Ha_user_approvePageInfo pageInfo) {
        for (Map<String, Object> v : result) {
            // 根据所需节点重新赋值
            if(departid.equals(v.get("id"))) {
                result = new ArrayList<>(10);
                result.add(v);
            }

            if(null != departUserMap.get(v.get("id"))) {
                List<Map<String, Object>> childrenList = (List<Map<String, Object>>) v.get("children");
                if(!CollectionUtils.isEmpty(childrenList)) {
                    if(!departid.equals(v.get("id"))) {
                        result = this.recursion(departid, childrenList, departUserMap, pageInfo);
                    }
                }
                Map<String, Object> tempMap;
                // 重新组装人员属性
                List<Map> userList = departUserMap.get(v.get("id"));
                List<Map<String, Object>> tempList = new ArrayList<>(userList.size());
                for (Map ha_user : userList) {
                    tempMap = new HashMap<>(16);
                    tempMap.put("id", ha_user.get("userid"));
                    tempMap.put("name", ha_user.get("fullname"));
                    tempMap.put("parentid", ha_user.get(pageInfo.getJoinfield()));
                    tempMap.put("type", "user");
                    tempMap.put("avatar", ha_user.get("avatar"));
                    tempList.add(tempMap);
                }
                childrenList.addAll(tempList);
            }
        }
        return result;
    }

}
